package ochart.core
{
	import ochart.elements.axes.RightYAxis;
	import ochart.elements.axes.Tick;
	import ochart.elements.axes.XAxis;
	import ochart.elements.axes.YAxis;
	import ochart.utils.ArrayUtil;

	public class XYChart extends ChartBase
	{
		public static const CLASS_NAME:String = "AxisChart";
		
		override public function get className():String
		{
			return CLASS_NAME;
		}
		
		public static const ALIAS:String = "axisChart";
		
		override public function get alias():String
		{
			return ALIAS;
		}
		
		public var hasBar:Boolean = false;
		
		protected var isXAxisChange:Boolean = false;
		private var _xAxis:XAxis = null;

		public function get xAxis():XAxis
		{
			return _xAxis;
		}

		public function set xAxis(value:XAxis):void
		{
			_xAxis = value;
			
			isXAxisChange = true;
			needUpdate = true;
		}
		
		protected var isYAxisChange:Boolean = false;
		private var _yAxis:YAxis = null;

		public function get yAxis():YAxis
		{
			return _yAxis;
		}

		public function set yAxis(value:YAxis):void
		{
			_yAxis = value;
			
			isYAxisChange = true;
			needUpdate = true;
		}
		
		protected var isRightYAxisChange:Boolean = false;
		private var _rightYAxis:RightYAxis = null;

		public function get rightYAxis():RightYAxis
		{
			return _rightYAxis;
		}

		public function set rightYAxis(value:RightYAxis):void
		{
			_rightYAxis = value;

			isRightYAxisChange = true;
			needUpdate = true;
		}
		
		private var defaultValue:Object = 
		{
			
		}
		
		private var _propertiesArray:Array = null;
			
		override public function get propertiesArray():Array
		{
			if(_propertiesArray)
			{
				return _propertiesArray;
			}
			
			_propertiesArray = super.propertiesArray;
			for(var name:String in defaultValue)
			{
				if(!ArrayUtil.containsValue(_propertiesArray, name))
				{
					_propertiesArray.push(name);
				}
			}
			return _propertiesArray;
		}

		public function XYChart()
		{
			super();
		}
		
		override public function getDefaultValue(name:String):*
		{
			return (defaultValue[name] === undefined) ? super.getDefaultValue(name) : defaultValue[name];
		}
		
		override public function update(callAfterUpdate:Boolean=true):Boolean
		{
			if(!super.update(false))
			{
				return false;
			}
			
			if(isXAxisChange)
			{
				axesContainer.removeChildByClassName(XAxis.CLASS_NAME);
				if(xAxis)
				{
					axesContainer.addChild(xAxis);
				}
			}
			if(isYAxisChange)
			{
				axesContainer.removeChildByClassName(YAxis.CLASS_NAME);
				if(yAxis)
				{
					axesContainer.addChild(yAxis);
				}
			}
			if(isRightYAxisChange)
			{
				axesContainer.removeChildByClassName(RightYAxis.CLASS_NAME);
				if(rightYAxis)
				{
					axesContainer.addChild(rightYAxis);
				}
			}
			if(isXAxisChange || isYAxisChange || isRightYAxisChange)
			{
				isXAxisChange = false;
				isYAxisChange = false;
				isRightYAxisChange = false;
				
				if(xAxis)
				{
					xAxis.y = height - xAxis.measureHeight;
				}
				if(yAxis)
				{
					yAxis.x = 0;
				}
				
				if(xAxis)
				{
					var xAxisX:Number = 0;
					if(yAxis)
					{
						xAxisX = yAxis.x + yAxis.centerX;
					}
					xAxis.x = xAxisX;
				}
				if(yAxis)
				{
					var yAxisY:Number = height - yAxis.height - yAxis.bottom;
					if(xAxis)
					{
						yAxisY = xAxis.y + xAxis.middleY - yAxis.height - yAxis.bottom;
					}
					yAxis.y = yAxisY;
				}
				if(rightYAxis)
				{
					var rightYAxisX:Number = 0;
					var rightYAxisY:Number = height - rightYAxis.height - rightYAxis.bottom;
					if(xAxis)
					{
						rightYAxisX = xAxis.x + xAxis.width + rightYAxis.left - rightYAxis.centerX;
						rightYAxisY = xAxis.y + xAxis.middleY - rightYAxis.height - rightYAxis.bottom;
					}
					rightYAxis.x = rightYAxisX;
					rightYAxis.y = rightYAxisY;
				}
			}
			
			if(isElementChange)
			{
				isElementChange = false;
				elementsContainer.clear();
				for each(var element:ChartObject in elementsArray)
				{
					element.x = xAxis.x;
					element.y = xAxis.y - element.totalHeight;
					elementsContainer.addChildAt(element, 0);
				}
			}
			
			if(callAfterUpdate)
			{
				return afterUpdate();
			}
			return true;
		}
	}
}